package com.company.project.service.impl;

import com.company.project.constant.TradeEnums;
import com.company.project.dao.TradeUserMapper;
import com.company.project.dao.TradeUserMoneyLogMapper;
import com.company.project.model.TradeUser;
import com.company.project.model.TradeUserMoneyLog;
import com.company.project.protocol.user.ChangeUserMoneyReq;
import com.company.project.protocol.user.ChangeUserMoneyRes;
import com.company.project.protocol.user.QueryUserReq;
import com.company.project.protocol.user.QueryUserRes;
import com.company.project.service.TradeUserService;
import com.company.project.core.AbstractService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;


/**
 * Created by CodeGenerator on 2018/12/07.
 */
@Service
@Transactional
public class TradeUserServiceImpl extends AbstractService<TradeUser> implements TradeUserService {

    @Resource
    TradeUserMapper tradeUserMapper;

    @Resource
    TradeUserMoneyLogMapper tradeUserMoneyLogMapper;

    public QueryUserRes queryUser(QueryUserReq queryUserReq) {
        QueryUserRes qrueryUserRes = new QueryUserRes();
        qrueryUserRes.setRetCode(TradeEnums.RetEnum.SUCCESS.getCode());
        qrueryUserRes.setRetInfo(TradeEnums.RetEnum.SUCCESS.getDesc());
        try {
            if (queryUserReq == null || queryUserReq.getUserId() == null) {
                throw new RuntimeException("请求参数不正确");
            }

            TradeUser tradeUser = this.findById(queryUserReq.getUserId());
            if (tradeUser != null) {
                BeanUtils.copyProperties(tradeUser, qrueryUserRes);
            } else {
                throw new RuntimeException("未查询到该用户");
            }
        } catch (Exception ex) {
            qrueryUserRes.setRetCode(TradeEnums.RetEnum.FAIL.getCode());
            qrueryUserRes.setRetInfo(ex.getMessage());
        }
        return qrueryUserRes;
    }

    //幂等操作 业务去重
    public ChangeUserMoneyRes changeUserMoney(ChangeUserMoneyReq changeUserMoneyReq) {
        ChangeUserMoneyRes changeUserMoneyRes = new ChangeUserMoneyRes();
        changeUserMoneyRes.setRetCode(TradeEnums.RetEnum.SUCCESS.getCode());
        changeUserMoneyRes.setRetInfo(TradeEnums.RetEnum.SUCCESS.getDesc());
        if (changeUserMoneyReq == null || changeUserMoneyReq.getUserId() == null
                || changeUserMoneyReq.getUserMoney() == null) {
            throw new RuntimeException("请求参数不正确");
        }
        if (changeUserMoneyReq.getUserMoney().compareTo(BigDecimal.ZERO) <= 0) {
            throw new RuntimeException("金额不能小于0");
        }
        TradeUserMoneyLog tradeUserMoneyLog = new TradeUserMoneyLog();
        tradeUserMoneyLog.setOrderId(changeUserMoneyReq.getOrderId());
        tradeUserMoneyLog.setUserId(changeUserMoneyReq.getUserId());
        tradeUserMoneyLog.setUserMoney(changeUserMoneyReq.getUserMoney());
        tradeUserMoneyLog.setCreateTime(new Date());
        tradeUserMoneyLog.setMoneyLogType(changeUserMoneyReq.getMoneyLogType());

        TradeUser tradeUser = new TradeUser();
        tradeUser.setUserId(changeUserMoneyReq.getUserId());
        tradeUser.setUserMoney(changeUserMoneyReq.getUserMoney());

        // 查询是否有订单付款记录
        TradeUserMoneyLog conditionLog = new TradeUserMoneyLog();
        conditionLog.setUserId(changeUserMoneyReq.getUserId());
        conditionLog.setOrderId(changeUserMoneyReq.getOrderId());
        conditionLog.setMoneyLogType(TradeEnums.UserMoneyLogTypeEnum.PAID.getCode());

        long count = this.tradeUserMoneyLogMapper.countByCondition(conditionLog);
        // 订单付款
        if (changeUserMoneyReq.getMoneyLogType().equals(TradeEnums.UserMoneyLogTypeEnum.PAID.getCode())) {
            if (count > 0) {
                throw new RuntimeException("已经付过款了，不能再付款");
            }
            tradeUserMapper.reduceUserMoney(tradeUser);
        }
        // 订单退款
        if (changeUserMoneyReq.getMoneyLogType().equals(TradeEnums.UserMoneyLogTypeEnum.REFUND.getCode())) {
            if (count == 0) {
                throw new RuntimeException("没有付款信息，不能退款");
            }
            conditionLog.setMoneyLogType(TradeEnums.UserMoneyLogTypeEnum.REFUND.getCode());
            count = this.tradeUserMoneyLogMapper.countByCondition(conditionLog);
            if (count > 0) {
                throw new RuntimeException("已经退过款了，不能退款");
            }
            tradeUserMapper.addUserMoney(tradeUser);
        }
       this.tradeUserMoneyLogMapper.insert(tradeUserMoneyLog);
       return changeUserMoneyRes;
    }


}
